package cn.xiaohuodui.component;

import cn.xiaohuodui.dao.SessionMapper;
import cn.xiaohuodui.dao.SmsMapper;
import cn.xiaohuodui.dao.User2Mapper;
import cn.xiaohuodui.form.user.UserPhoneForm;
import cn.xiaohuodui.model.Session;
import cn.xiaohuodui.model.Sms;
import cn.xiaohuodui.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * Author :keepcleargas
 * Date   :2016-12-16 12:47.
 */
@Component
public class UserComponent {
    public final static long EXPIRED = 1000l * 3600 * 24 * 30;

    @Autowired
    User2Mapper user2Mapper;
    @Autowired
    SmsMapper smsMapper;
    @Autowired
    SessionMapper userSessionMapper;

    /**
     * 插入Sms 记录
     *
     * @param phone
     * @param code
     */
    public void insertSms(String phone, String code) {
        Sms sms = new Sms();
        sms.setCode(code);
        sms.setPhone(phone);
        sms.setCreated(System.currentTimeMillis());
        smsMapper.insert(sms);
    }

    /**
     * 验证短信
     *
     * @param phone
     * @param code
     * @return
     */
    public int validateSms(String phone, String code) {
        Sms sms = smsMapper.selectLastSms(phone);
        if (sms == null) {
            return 0;
        }

        if (!sms.getCode().equals(code)) {
            return 0;
        }
        return sms.getId();
    }

    /**
     * 设置短信已被验证
     *
     * @param sid
     */
    public void markSmsUsed(int sid) {
        smsMapper.markUsed(sid);
    }


    /**
     * 插入新用户
     *
     * @param userPhoneForm
     * @return
     */
    public User insertUser(UserPhoneForm userPhoneForm) {
        User user = new User();
//        user.setPhone(userPhoneForm.getPhone());
        user.setAvatar(userPhoneForm.getAvatar());
        if (StringUtils.isEmpty(userPhoneForm.getNickname())) {
            //昵称初始化 为用户_13175060xxx
            user.setNickname("u" + userPhoneForm.getPhone().substring(0, 7) + "****");
        } else {
            user.setNickname(userPhoneForm.getNickname());
        }
//        user.setSalt(userPhoneForm.getSalt());
        user.setPassword(userPhoneForm.getHashPwd());

        long created = System.currentTimeMillis();
        user.setCreated(created);
        user.setUpdated(created);

        user2Mapper.insert(user);
        return user;
    }

    /**
     * 新建一个 用户Session
     * <p/>
     * uid platform 唯一
     *
     * @param uid
     * @param token
     */
    public void newSession(int uid, int platform, String device, String token) {
        Session userSession = userSessionMapper.selectByUidAndExpired(uid, System.currentTimeMillis());
        if (userSession != null) {
            userSessionMapper.selectByUidAndExpired(uid, System.currentTimeMillis());
        }
        Session us = new Session();
        long created = System.currentTimeMillis();
        us.setUid(uid);
        us.setDevice(device);
        us.setToken(token);
        us.setPlatform(platform);
        us.setCreated(created);
        us.setExpired(created + EXPIRED);
        userSessionMapper.insert(us);
    }

    /**
     * 设置该用户的 所有token 都为过期了
     *
     * @param uid
     */
    public void setSessionExpired(int uid) {
        userSessionMapper.selectByUidAndExpired(uid, System.currentTimeMillis());
    }

    /**
     * 获取Session
     *
     * @param uid
     * @return
     */
    public Session findSession(int uid) {
        Session userSession = userSessionMapper.selectByUidAndExpired(uid, System.currentTimeMillis());
        return userSession;
    }


    /**
     * 获取用户 最后的登录平台
     *
     * @param uid
     * @return
     */
    public Session findLastSession(int uid) {
        return userSessionMapper.selectLastSession(uid);
    }
}
